package com.flowerfling;

import static com.flowerfling.FlingConstant.GAME_MAIN_CLS;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Base64;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class FlingSDKManager implements Application.ActivityLifecycleCallbacks {

    public static FlingSDKManager getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final FlingSDKManager GAME_APPLICATION = new FlingSDKManager();
    }

    public void init(Activity activity) {
        Context mApplication = activity.getApplicationContext();
        ((Application) mApplication).registerActivityLifecycleCallbacks(this);
        FlingCocosBridge.getInstance().init(mApplication);

        setPrivacySettings(mApplication);
        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(
                        decrypt(FlingConstant.MAX_KEY), mApplication)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        AppLovinSdk.getInstance(mApplication).initialize(initConfig, config -> {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
            FlingMaxReward.getInstance().loadRewardAd(mApplication);
        });
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        try {
            if (activity.getClass().getName().equals(GAME_MAIN_CLS)) {
                FlingCocosBridge.getInstance().setMainGameActivity(activity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }

    // 加密方法
//    public static String encrypt(String plainText) {
//        try {
//            IvParameterSpec ivSpec = new IvParameterSpec(FlingConstant.IV.getBytes("UTF-8"));
//            SecretKeySpec keySpec = new SecretKeySpec(FlingConstant.KEY.getBytes("UTF-8"), "AES");
//
//            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // 使用 CBC 模式和 PKCS5Padding
//            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
//
//            byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
//            // 使用 android.util.Base64 进行编码
//            return Base64.encodeToString(encrypted, Base64.NO_WRAP);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return "";
//    }

    // 解密方法
    public static String decrypt(String cipherText)  {
        try {
            IvParameterSpec ivSpec = new IvParameterSpec(FlingConstant.IV.getBytes("UTF-8"));
            SecretKeySpec keySpec = new SecretKeySpec(FlingConstant.KEY.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // 使用 CBC 模式和 PKCS5Padding
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

            // 使用 android.util.Base64 进行解码
            byte[] decoded = Base64.decode(cipherText, Base64.NO_WRAP);
            byte[] original = cipher.doFinal(decoded);

            return new String(original, "UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    private void setPrivacySettings(Context mApplication) {
        AppLovinPrivacySettings.setHasUserConsent(true, mApplication);
        AppLovinPrivacySettings.setDoNotSell(false, mApplication);
    }

}
